/*******************************************************************************
* Copyright (C) 2019 China Micro Semiconductor Limited Company. All Rights Reserved.
*
* This software is owned and published by:
* CMS LLC, No 2609-10, Taurus Plaza, TaoyuanRoad, NanshanDistrict, Shenzhen, China.
*
* BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
* BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
*
* This software contains source code for use with CMS
* components. This software is licensed by CMS to be adapted only
* for use in systems utilizing CMS components. CMS shall not be
* responsible for misuse or illegal use of this software for devices not
* supported herein. CMS is providing this software "AS IS" and will
* not be responsible for issues arising from incorrect user implementation
* of the software.
*
* This software may be replicated in part or whole for the licensed use,
* with the restriction that this Disclaimer and Copyright notice must be
* included with each copy of this software, whether used in part or whole,
* at all times.
*/

#ifndef __MCU_CONFIG_H__
#define __MCU_CONFIG_H__

/****************************************************************************/
/*	Local type definitions('typedef')
*****************************************************************************/

typedef  unsigned long int						u64;
typedef  unsigned int							u32;
typedef  unsigned short int						u16;
typedef  unsigned char							u8;
typedef  signed  long int						s64;
typedef  signed int							s32;
typedef  signed short int						s16;
typedef  signed char							s8;

/****************************************************************************/
/*	Local pre-processor symbols/macros('#define')
*****************************************************************************/

/*
MCU TYPE
*/ 
#define MCU_TYPE                                       MX32M5333//MX32M5332// MX32M5333	

#define MX32M5332                                       1       
#define MX32M5333                                       2    


/*
MCU FRE
*/
#define MCU_CORE_CLK                         	(48ul)					//MHZ	
/*
SYS_TICK
*/
#define SYSTICK_TIMES	                        (1000ul)																							//US
#define SYSTICK_TIMES_CNT                        (0XFFFFFF&(MCU_CORE_CLK*SYSTICK_TIMES))								
#define DISABLE_SYSTICK_INT()	                (SysTick->CTRL &= 0XFFFFFFFD)
#define READ_SYSTICK_COUNTF()					(SysTick->CTRL & 0X00010000)

/*
GPIO
*/
#define DDBS_IO
//#define YLGJ_IO
//#define DRILL_IO

/*
ADC
*/
#define ADC1_CLK_DIV_VALUE						ADC1_CLK_DIV_2
#define ADC1_CLK_FILTER_VALUE					ADC1_HOLD_3P5_CLK



#ifdef DDBS_IO
//#define ADC1_CH_SUM                             5   

//#define CH_IAVG                                 ADC1_CH_24_MSK
//#define RESULT_IAVG                             ADC1_CH_24
//#define CH_VBUS                                 ADC1_CH_15_MSK
//#define RESULT_VBUS                             ADC1_CH_15
//#define CH_NTC1                             ADC1_CH_7_MSK	 //bat temperature
//#define RESULT_NTC1                         ADC1_CH_7
//#define CH_NTC2                             ADC1_CH_20_MSK  //mos temperature
//#define RESULT_NTC2                         ADC1_CH_20
//#define CH_VSP               	                ADC1_CH_17_MSK
//#define RESULT_VSP                              ADC1_CH_17

#define ADC1_CH_SUM                             4  

#define CH_IAVG                                 ADC1_CH_14_MSK
#define RESULT_IAVG                             ADC1_CH_14
#define CH_VBUS                                 ADC1_CH_27_MSK
#define RESULT_VBUS                             ADC1_CH_27

#define CH_NTC1                             ADC1_CH_7_MSK	 //bat temperature
#define RESULT_NTC1                         ADC1_CH_7
#define CH_NTC2                             ADC1_CH_20_MSK  //mos temperature
#define RESULT_NTC2                         ADC1_CH_20

#define CH_KEY                             ADC1_CH_18_MSK  //mos temperature
#define RESULT_KEY                         ADC1_CH_18

#define CH_VSP               	                ADC1_CH_25_MSK
#define RESULT_VSP                              ADC1_CH_25

#endif



#define CH_O                                    ADC1_CH_26_MSK
#define RESULT_O                                ADC1_CH_26
#define CH_U                                    ADC1_CH_8_MSK
#define RESULT_U                                ADC1_CH_8
#define CH_V                                    ADC1_CH_9_MSK
#define RESULT_V                                ADC1_CH_9
#define CH_W                                    ADC1_CH_10_MSK
#define RESULT_W                                ADC1_CH_10
#define CH_IPEAK                                ADC1_CH_30_MSK
#define RESULT_IPEAK                           	ADC1_CH_30

#define ADC1_PWM_HW_ON                          ADC1_EnableHardwareTigger(ADC1_TG_EPWM_CMP1)
#define ADC1_PWM_HW_OFF                         ADC1_DisableHardwareTigger(ADC1_TG_EPWM_CMP1)

/*
PWM
*/
#define PWM_CMP1_TO_TRGADC1_TIME				8	//	300//96//300--cmp		//48--1US
#define PWM_DEAT_TIME							48//32		//48--1US


/*
T0&T1
*/

#define T0_STOP()								TMR_Stop(TMR0)
#define T0_RUN()								TMR_Start(TMR0)
#define T1_STOP()								TMR_Stop(TMR1)
#define T1_RUN()								TMR_Start(TMR1)
#define T1_IE_DIS()		
#define Load_T0_Cnt()							(0xffffffff - TMR_GetTimingData(TMR0))   
#define Reset_T0_Cnt()							TMR_SetPeriod(TMR0,0XFFFFFFFF)
#define Write_T0_Cnt(x)							TMR_SetPeriod(TMR0,(0XFFFFFFFF -x));	TMR_Start(TMR0)
#define Write_T1_Cnt(x)							TMR_SetPeriod(TMR1,x);	TMR_Start(TMR1)
/*
CMP
*/
#define BEMF_U									ACMP_POSSEL_P0
#define BEMF_V									ACMP_POSSEL_P1
#define BEMF_W									ACMP_POSSEL_P2
#define CMP0_IF_CLR()							ACMP_ClearIntFlag(ACMP0)
#define CMP0_OUT()								ACMP_GetResult(ACMP0)
#define CMP0_INT_DIS()							ACMP_DisableInt(ACMP0)
#define CMP0_INT_EN()							ACMP_EnableInt(ACMP0)
#define CMP1_TO_SHORT_CURT_FILTERS				ACMP_NGCLK_65_TSYS
#define CMP0_TO_BEMF_FILTERS					ACMP_NFCLK_129_TSYS//ACMP_NGCLK_65_TSYS

/*
*UART
*/
#define UART_THREIE_EN_Pos						(1)
#define UART_THREIE_EN_Msk						(0x01<<UART_THREIE_EN_Pos)
#define UART_RBRIE_EN_Pos						(0)
#define UART_RBRIE_EN_Msk						(0x01<<UART_RBRIE_EN_Pos)

#define UART_INTID_RX_Pos			(2)
#define UART_INTID_RX_Msk			(0x01<<UART_INTID_RX_Pos)
#define UART_INTID_TX_Pos			(1)
#define UART_INTID_TX_Msk			(0x01<<UART_INTID_TX_Pos)

#define UART_EnableIRQ(Msk)              UART1->IER |= (Msk)
#define UART_DisableIRQ(Msk)             UART1->IER &= ~(Msk)


/*
INTERRUPT
*/
#define    DI()                                __disable_irq()
#define    EI()                                 __enable_irq()

/****************************************************************************/
/*	Global variable definitions(declared in header file with 'extern')
*****************************************************************************/
extern uint32_t Adc1ConvertIndex[ADC1_CH_SUM];


void BSP_SetInt_Priority(void);
void BSP_Enable_Interrup(void);
void BSP_Disable_Interrup(void);
void BSP_MCU_Config(void);
void BSP_GPIOConfig(void);
void BSP_ADC1Config(void);
void BSP_Adc1SingleConvet(unsigned int ChannelMask);
uint16_t  BSP_Adc1_3TimesConvet(unsigned int ChannelMask);
void BSP_ADC1ClearAllChnInt(void);
void BSP_TMR0Config(void);
void BSP_TMR1Config(void);
void BSP_ACMP0Config(void);
void BSP_ACMP1Config(void);
void BSP_OPA1Config(void);
void BSP_RPPPGA0Config(void);
void BSP_PWMConfig(void);
void BSP_PWMPOREConfig(void);

void BSP_ClearBreakIntFlag(void);
void BSP_ADCConvetInAdcISR(void);
void BSP_UART1Config(void);
void BSP_CCP1Config(void);
void BSP_CCP0Config(void);
void BSP_SetCCP0Period( uint16_t Period);
void BSP_PGA0Config(void);
void BSP_PGA1Config(void);
void BSP_WDTConfig(void);
void BSP_Delay_Us(unsigned int us);
void BSP_Delay_Ms(unsigned int ms);

void BSP_PWMcomplmtConfig(void);

#endif /* __MCU_CONFIG_H__ */

